package com.java.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import com.java.dao.TodoDao;
import com.java.entity.TodoList;
import com.java.ext.MyButton;
import com.java.utils.MyWindowListener;

/**
* @ClassName: AdminPasswordWindow
* @Description: 管理员修改密码
*
 */
public class TodoDrawWindow extends BaseJFrame {
	private static final long serialVersionUID = 1L;

	/**
	 * 父级窗口
	 */
	JFrame pJFrame;

	JFrame thisFrame = this;

	// 本界面组件
	MyButton define = new MyButton("Search").primary();
	MyButton preMonth = new MyButton("Pre month").defaultt();
	MyButton nextMonth = new MyButton("Next month").defaultt();
	MyButton reback = new MyButton("Back").warning();
	JLabel getYear = new JLabel("Y");
	JLabel getMonth = new JLabel("M");
	JTextField year = new JTextField(5);
	JTextField month = new JTextField(5);
	static JLabel Time = new JLabel();
	JLabel Dep = new JLabel();
	JPanel jp2 = new JPanel();
	
	Map<String, List<TodoList>>  dayMap = new HashMap<String, List<TodoList>>();

	// 构造函数
	public TodoDrawWindow(JFrame bJFrame) {
		pJFrame = bJFrame;
		Container maxBox = this.getContentPane();
		// 设置窗口属性
		setTitle("Calendar View");
		setSize(650, 350);
		setResizable(false);
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		addWindowListener(new CloseButtonListener());
		setLocationRelativeTo(null);
		
		dayMap = TodoDao.getMap();

		// 创建用于布局的 JPanel
		JPanel contentPane = new JPanel();
		contentPane.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
		contentPane.setLayout(new GridLayout(3, 2)); // 使用网格布局管理器

		// 显示日历
		MyButton j1 = new MyButton("Monday").defaultt();
		MyButton j2 = new MyButton("Tuesday").defaultt();
		MyButton j3 = new MyButton("Wednesday").defaultt();
		MyButton j4 = new MyButton("Thursday").defaultt();
		MyButton j5 = new MyButton("Friday").defaultt();
		MyButton j6 = new MyButton("Saturday").defaultt();
		MyButton j7 = new MyButton("Sunday").defaultt();
		this.setLayout(new FlowLayout());
		JPanel jp = new JPanel();
		jp.add(getYear);
		jp.add(year);
		jp.add(getMonth);
		jp.add(month);
		jp.add(define);
		define.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if ("".equals(year.getText()) || "".equals(month.getText())) {
					error("Null Text");
					return;
				}
				try {
					int Y = Integer.parseInt(year.getText());
					int M = Integer.parseInt(month.getText());
					DrawStart.nowMonth = M;
					DrawStart.nowYear = Y;
					jp2.removeAll();
					jp2.repaint();
					showCalendar();
				} catch (Exception e2) {
					error(e2.getMessage());
					return;
				}
			}

		});
		jp.add(preMonth);
		preMonth.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (DrawStart.nowMonth == 0) {
					DrawStart.nowMonth = 12;
					DrawStart.nowYear -= 1;
				}
				DrawStart.nowMonth -= 1;
				jp2.removeAll();
				jp2.repaint();
				showCalendar();
			}

		});
		jp.add(nextMonth);
		jp.add(reback);
		nextMonth.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (DrawStart.nowMonth == 12) {
					DrawStart.nowMonth = 0;
					DrawStart.nowYear += 1;
				}
				DrawStart.nowMonth += 1;
				jp2.removeAll();
				jp2.repaint();
				showCalendar();
			}

		});
		reback.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setVisible(false);
				pJFrame.setEnabled(true);
			}

		});
		jp.add(Time);
		maxBox.add(jp);
		JPanel jp3 = new JPanel();
		jp3.setLayout(new FlowLayout());
		jp3.add(j1);
		jp3.add(j2);
		jp3.add(j3);
		jp3.add(j4);
		jp3.add(j5);
		jp3.add(j6);
		jp3.add(j7);
		showCalendar();
		jp2.setLayout(null);
		jp2.setLayout(new GridLayout(6, 7, 9, 5));
		maxBox.add(jp3);
		maxBox.add(jp2);
		maxBox.add(Dep);

		// 将按钮面板添加到主窗口的底部
		getContentPane().add(contentPane, BorderLayout.CENTER);

		setVisible(true);

	}

	public void showCalendar() {
		DrawStart.nowTime.set(Calendar.DATE, 1);
		DrawStart.nowDay = DrawStart.nowTime.get(Calendar.DATE);
		int count = 1;
		int weekDay = DrawStart.getFirstWeekDay(DrawStart.nowYear, DrawStart.nowMonth, DrawStart.nowDay);
		int monthDay = DrawStart.getMonthDays(DrawStart.nowYear, DrawStart.nowMonth);
		int preMonthDay = DrawStart.getMonthDays(DrawStart.nowYear, DrawStart.nowMonth - 1);
		int preDay = preMonthDay - weekDay + 2;
		for (int i = 1; i <= 42; i++) {
			JTextField j = new JTextField(7);
			j.setEditable(false);
			j.setHorizontalAlignment(SwingConstants.CENTER);
			if (i >= weekDay && (i - weekDay + 1) <= monthDay) {
				j.setForeground(Color.BLACK);
				int day = (i - weekDay + 1);
				j.setText("" + day);
				int tmon = DrawStart.nowMonth;
				String date = DrawStart.nowYear + "-" + (tmon < 10? "0" + tmon:tmon) + "-" + (day < 10? "0" + day:day);
				if(null != dayMap.get(date)) {
					j.setForeground(Color.RED);
				}
			} else if (i < weekDay) {
				j.setText("" + preDay);
				j.setForeground(Color.GRAY);
				preDay++;
			} else {
				j.setText(String.valueOf(count));
				j.setForeground(Color.GRAY);
				count++;
			}
			jp2.add(j);
		}
		Dep.setText("Current date：" + DrawStart.nowYear + "-" + DrawStart.nowMonth);
	}

	// 显示当前时间
	public static void showNowTime() {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Time.setText(sdf.format(new Date()));
			Thread.sleep(1000);
		} catch (InterruptedException exception) {
			System.out.println("error");
		}
	}

	class CloseButtonListener extends MyWindowListener {

		@Override
		public void windowClosing(WindowEvent e) {
			// setVisible(false);
			// pJFrame.setEnabled(true);
		}
	}

}